package com.zs.zcerp.approval.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.zcerp.approval.entity.*;
import com.zs.zcerp.approval.mapper.QhChkmgrdMapper;
import com.zs.zcerp.approval.service.IQhBasestdMService;
import com.zs.zcerp.approval.service.IQhChkmgrdService;
import com.zs.zcerp.common.entity.FebsConstant;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.utils.DateUtil;
import com.zs.zcerp.common.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Henry
 *
 * @author admin
 */

@Service
@RequiredArgsConstructor

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class QhChkmgrdServiceImpl extends ServiceImpl<QhChkmgrdMapper, QhChkmgrdEntity> implements IQhChkmgrdService {

    private final IQhBasestdMService iQhBasestdMService;

    @Override
    @DS("oracle") //从oracle库数据查询
    public IPage<QhChkmgrdEntity> findApprovalsDetailList(QhChkmgrdEntity qhChkmgrdEntity, QueryRequest request) {
        if (StringUtils.isNotBlank(qhChkmgrdEntity.getCreateTimeFrom()) &&
                StringUtils.equals(qhChkmgrdEntity.getCreateTimeFrom(), qhChkmgrdEntity.getCreateTimeTo())) {
            qhChkmgrdEntity.setCreateTimeFrom(DateUtil.formatDateTime(qhChkmgrdEntity.getCreateTimeFrom()));
            qhChkmgrdEntity.setCreateTimeTo(DateUtil.formatDateTime(qhChkmgrdEntity.getCreateTimeTo()));
        }
        Page<QhChkmgrdEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countAprovalsDetail(qhChkmgrdEntity));
        SortUtil.handlePageSort(request, page, "sendDate", FebsConstant.ORDER_ASC, false);
        return baseMapper.findAprovalsDetailPage(page, qhChkmgrdEntity);
    }


    @Override
    @DS("oracle") //从oracle库数据查询
    @Transactional(rollbackFor = Exception.class)
    public void updateaAprovals(QhChkmgrdEntity qhChkmgrdEntity) {
        /**当前用户签核*/
        qhChkmgrdEntity.setFactEtime(new Date());
        qhChkmgrdEntity.setChkDate(new Date());
        baseMapper.updateChkStatus(qhChkmgrdEntity);

        /**把下一个签核用户 设置当前节点*/
        //baseMapper.updateNoteId(qhChkmgrdEntity);
    }


    @Override
    @DS("oracle") //从oracle库数据查询
    public void updateaMainSign(QhChkmgrdEntity qhChkmgrdEntity) {
        qhChkmgrdEntity.setFactEtime(new Date());
        qhChkmgrdEntity.setChkDate(new Date());
        /**   明细表当前步骤签核   */
        baseMapper.updateChkStatus(qhChkmgrdEntity);
        /**   主表当前步骤签核  */
        baseMapper.updateStatus(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public void deleteByOrderyNo(String orderyNo) {
        baseMapper.delete(new QueryWrapper<QhChkmgrdEntity>().eq("ORDER_NO",orderyNo));

    }



    @Override
    @DS("oracle") //从oracle库数据查询
    public void updateStatus(QhChkmgrdEntity qhChkmgrdEntity) {

        baseMapper.updateChkStatus(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public void updateByToUser(QhChkmgrdEntity qhChkmgrdEntity, String toUser) {
        baseMapper.update(qhChkmgrdEntity, new QueryWrapper<QhChkmgrdEntity>().eq("TO_USER", toUser).eq("CHK_STATUS", 1));
    }


    @Override
    @DS("oracle") //从oracle库数据查询
    public List<String> getOrderList(QhBasestdDEntity q) {
      return   baseMapper.getOrderList(q);
    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public void updateToUserAndNextUser(QhChkmgrdEntity qhChkmgrdEntity, List<String> orderList) {
        this.baseMapper.update(qhChkmgrdEntity, new QueryWrapper<QhChkmgrdEntity>().eq("CHK_STATUS", 1).in("ORDER_NO",orderList));
    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public void upadteNextUser(String oldNextUser, String newNextUser) {
        QhChkmgrdEntity qhChkmgrdEntity = new QhChkmgrdEntity();
        qhChkmgrdEntity.setNextUser(newNextUser);
        baseMapper.update(qhChkmgrdEntity, new QueryWrapper<QhChkmgrdEntity>().eq("Next_User", oldNextUser).eq("CHK_STATUS", 1));
    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public void destorySignStatus(String orderNo) {
       baseMapper.destorySignStatus(orderNo);
    }




    @Override
    @DS("oracle") //从oracle库数据查询
    public void rejectDestoryStatus(String orderNo) {
        baseMapper.rejectDestoryStatus(orderNo);
    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public List<DestoryReportEntity> findListReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findListReport(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public List<ADReportEntity> findAdReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findAdReport(qhChkmgrdEntity);

    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public List<ADReportEntity> findAdReportTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findAdReportTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public SeReportEntity findSeReportTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findSeReportTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public List<SeReportEntity> findSeReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findSeReport(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2") //从oracle库数据查询
    public List<OrderReportEntity> findOrderReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findOrderReport(qhChkmgrdEntity);
    }


    /***********************************采购单开始 **************henry  2021-08-26********  */

    @Override
    @DS("oracle2") //从oracle库数据查询
    public OrderFatrEntity findFatrOrderTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findFatrOrderTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2") //从oracle库数据查询
    public List<OrderFatrEntity> findFatrReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findFatrReport(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2") //从oracle库数据查询
    public List<OrderFatrEntity> findFainReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findFainReport(qhChkmgrdEntity);
    }
    @Override
    @DS("oracle2") //从oracle库数据查询
    public OrderXPOEntity findXPOOrderTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findXPOOrderTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2") //从oracle库数据查询
    public List<OrderXPOEntity> findXPOReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findXPOReport(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public OrderXPCEntity findXPCOrderTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findXPCOrderTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public List<OrderXPCEntity> findXPCReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findXPCReport(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public KFPOReportEntity findKFPOTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findKFPOTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public List<KFPOReportEntity> findKFPOReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findKFPOReport(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public KFPRReportEntity findKFPRTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findKFPRTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public List<KFPRReportEntity> findKFPRReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findKFPRReport(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public KFPOReportEntity findKFPO2Title(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findKFPO2Title(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public List<KFPOReportEntity> findKFPO2Report(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findKFPO2Report(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public WOReportEntity findWOTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findWOTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public List<WOReportEntity> findWOReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findWOReport(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle") //从oracle库数据查询
    public List<EmpEntity> querySignYesterDay() {
        return baseMapper.querySignYesterDay();
    }

    @Override
    @DS("oracle2")
    public List<SignDetail> selectDetail(String orderNo, String itemType, String orgId) {
        return baseMapper.selectDetail(orderNo,itemType,orgId);
    }



    /***********************************采购单 结束 **********************  */
    @Override
    @DS("oracle2")
    public GAPOReportEntity findGAPOTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findGAPOReportTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public List<GAPOReportEntity> findGAPOReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findGAPOReport(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public MDPOReportEntity findMDPOTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findMDPOReportTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public List<MDPOReportEntity> findMDPOReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findMDPOReport(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public PO6ReportEntity findPO6Title(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findPO6Title(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public List<PO6ReportEntity> findPO6Report(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findPO6Report(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public PGReportEntity findPGTitle(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findPGTitle(qhChkmgrdEntity);
    }

    @Override
    @DS("oracle2")
    public List<PGReportEntity> findPGReport(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findPGReport(qhChkmgrdEntity);
    }


    @Override
    @DS("oracle") //从oracle库数据查询
    public  List<QhChkmgrdEntity> findSignList(String orderNo){
        List<QhChkmgrdEntity> qhChkmgrdEntityList = baseMapper.findSignList(orderNo);
        if(qhChkmgrdEntityList.size()>0){
         String  basestdNo =  qhChkmgrdEntityList.get(0).getBasestdNo();
        String folwName =  iQhBasestdMService.selectByBasestdNo(basestdNo).getNameT();
            qhChkmgrdEntityList.get(0).setFlowName(folwName);
        }

        return   qhChkmgrdEntityList;
        }

    @Override
    @DS("oracle2")
    public OrderBPOTitleEntity findBPO19Title(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findBPO19Title(qhChkmgrdEntity);
    }
    @Override
    @DS("oracle2")
    public OrderBPOTitleEntity findBPO20Title(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findBPO20Title(qhChkmgrdEntity);
    }
    @Override
    @DS("oracle2")
    public List<OrderBPODetailEntity> findBPODetail(QhChkmgrdEntity qhChkmgrdEntity) {
        return baseMapper.findBPODetail(qhChkmgrdEntity);
    }
}
